package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.dto.*;
import com.example.homeworkmanagement.pojo.Teaches;
import com.example.homeworkmanagement.service.TeachesService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/teaches")
@Api(tags="教学安排管理",description="提供管理API接口")
@CrossOrigin
public class TeachesController {
    @Autowired
    private TeachesService teachesService;

    /**
     * 根据教学安排ID获取详情
     */
    @GetMapping("/getTeachesDetail")
    @ApiOperation(value = "根据教学安排ID获取详情")
    @ApiImplicitParam(name = "teachId", value = "教学安排ID", required = true, dataType = "integer")
    public ResultVO<TeachesAndClassesDTO> getTeachesDetail(Integer teachId) {
        return teachesService.getTeachesDetail(teachId);
    }

    /**
     * 获取教学安排列表（支持分页和条件查询）
     */
    @PostMapping("/getTeachesList")
    @ApiOperation(value = "获取教学安排列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程ID", required = false),
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师ID", required = false),
            @ApiImplicitParam(dataType = "string", name = "classIds", value = "班级ID列表", required = false),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码（从1开始）", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true, defaultValue = "10")
    })
    public ResultVO<List<Teaches>> getTeachesList(Teaches teaches, Integer page, Integer size) {
        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return teachesService.getTeachesList(teaches,page,size);
    }

    /**
     * 新增教学安排
     */

    @PostMapping("/addTeaches")
    @ApiOperation(value = "新增教学安排")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程ID", required = true),
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师ID", required = true),
            @ApiImplicitParam(dataType = "string", name = "classIds", value = "班级ID列表（逗号分隔）", required = true)
    })
    public ResultVO<Teaches> addTeaches(Teaches teaches) {
        return teachesService.addTeaches(teaches);
    }

    @PostMapping("/addTeaches2")
    @ApiOperation(value = "新增教学安排-班级名称串与课程名称")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "courseName", value = "课程名称", required = true),
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师ID", required = true),
            @ApiImplicitParam(dataType = "string", name = "classNames", value = "班级名称", required = true)
    })
    public ResultVO<Teaches> addTeaches2(String classNames,String courseName,String teacherId) {
        return teachesService.addTeaches2(classNames,courseName,teacherId);
    }

    /**
     * 更新教学安排
     */
    @PostMapping("/updateTeachesByTeachId")
    @ApiOperation(value = "更新教学安排")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "integer", name = "teachId", value = "教学安排ID", required = true),
            @ApiImplicitParam(dataType = "string", name = "courseId", value = "课程ID", required = false),
            @ApiImplicitParam(dataType = "string", name = "teacherId", value = "教师ID", required = false),
            @ApiImplicitParam(dataType = "string", name = "classIds", value = "班级ID列表（逗号分隔）", required = false)
    })
    public ResultVO<Teaches> updateTeachesByTeachId(Teaches teaches) {
        return teachesService.updateTeaches(teaches);
    }

    /**
     * 删除教学安排
     */
    @DeleteMapping("/deleteTeachesByTeachId")
    @ApiOperation(value = "删除教学安排")
    @ApiImplicitParam(name = "teachId", value = "教学安排ID", required = true, dataType = "integer")
    public ResultVO<Teaches> deleteTeachesByTeachId(Integer teachId) {
        return teachesService.deleteTeaches(teachId);
    }

    /**
     * 组合查询根据教师id查询教授的课程
     */
    @PostMapping("/getCourseTeachesInfoByTeacherId")
    @ApiOperation(value = "根据教师ID查询教学安排")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId", value = "教师ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "courseName", value = "课程名称", required = false, dataType = "string"),
            @ApiImplicitParam(name = "courseCredits", value = "课程学分", required = false, dataType = "string"),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码（从1开始）", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true, defaultValue = "10")
    })
    public ResultVO<List<CoursesAndTeachesAndClassesDTO>>getCourseTeachesInfoByTeacherId(String teacherId, String courseName, String courseCredits, Integer page, Integer size) {
        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return teachesService.getCourseTeachesInfoByTeacherId(teacherId,courseName,courseCredits,page,size);
    }

    /**
     * 组合查询根据班级ID查询班级开的课程
     */
    @GetMapping("/getInfoByClassId")
    @ApiOperation("根据班级ID查询课程信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "teacherName", value = "教师名称（模糊查询）", required = false, dataType = "string"),
            @ApiImplicitParam(name = "courseName", value = "课程名称（模糊查询）", required = false, dataType = "string"),
            @ApiImplicitParam(name = "courseCredits", value = "课程学分（模糊查询）", required = false, dataType = "string"),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码（从1开始）", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true, defaultValue = "10")
    })
    public ResultVO<List<CoursesAndTeachesAndTeachersAndClasssesDTO>> getInfoByClassId(String classId, String teacherName, String courseName, String courseCredits, Integer page, Integer size) {
        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return teachesService.getInfoByClassId(classId,teacherName,courseName,courseCredits,page,size);
    }

    /**
     * 根据教师id获取教学安排班级
     */
    @GetMapping("/getClassNamesListByTeacherId")
    @ApiOperation(value = "根据教师ID获取教学安排班级")
    @ApiImplicitParam(name = "teacherId", value = "教师ID", required = true, dataType = "string")
    public ResultVO<List<String>> getClassNamesListByTeacherId(String teacherId) {
        return teachesService.getClassNamesListByTeacherId(teacherId);
    }
}
